home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-12-04 | 6.3 KB | 185 lines |
- /*
- A basic extension of the java.applet.Applet class
- */
-
- import java.awt.*;
- import java.applet.*;
- import java.net.*;
- import java.io.*;
-
- public class EMail extends Applet {
- public void init() {
- super.init();
-
- //{{INIT_CONTROLS
- setLayout(null);
- addNotify();
- resize(650,530);
- setBackground(new Color(12632256));
- Title = new java.awt.Label("Simple E-Mail Applet",Label.CENTER);
- Title.reshape(169,12,312,38);
- Title.setFont(new Font("Dialog", Font.BOLD, 24));
- add(Title);
- keyPressManagerPanel1 = new symantec.itools.awt.KeyPressManagerPanel();
- keyPressManagerPanel1.setLayout(null);
- keyPressManagerPanel1.reshape(10,51,605,501);
- add(keyPressManagerPanel1);
- senderName = new java.awt.TextField();
- senderName.reshape(138,-1,456,24);
- keyPressManagerPanel1.add(senderName);
- recipientName = new java.awt.TextField();
- recipientName.reshape(138,36,456,24);
- keyPressManagerPanel1.add(recipientName);
- subjectLine = new java.awt.TextField();
- subjectLine.reshape(138,72,456,24);
- keyPressManagerPanel1.add(subjectLine);
- messageBody = new java.awt.TextArea();
- messageBody.reshape(138,108,456,228);
- keyPressManagerPanel1.add(messageBody);
- serverResponse = new java.awt.TextArea();
- serverResponse.reshape(138,348,456,84);
- keyPressManagerPanel1.add(serverResponse);
- Send = new java.awt.Button("Send");
- Send.reshape(138,441,108,24);
- keyPressManagerPanel1.add(Send);
- newButton = new java.awt.Button("New");
- newButton.reshape(486,441,108,24);
- keyPressManagerPanel1.add(newButton);
- senderLabel = new java.awt.Label("From:");
- senderLabel.reshape(14,2,96,23);
- keyPressManagerPanel1.add(senderLabel);
- messageLabel = new java.awt.Label("Message:");
- messageLabel.reshape(14,110,96,23);
- keyPressManagerPanel1.add(messageLabel);
- subjectLabel = new java.awt.Label("Subject:");
- subjectLabel.reshape(14,74,96,23);
- keyPressManagerPanel1.add(subjectLabel);
- serverRepsonseLabel = new java.awt.Label("Server Response:");
- serverRepsonseLabel.reshape(14,351,132,24);
- keyPressManagerPanel1.add(serverRepsonseLabel);
- recipientLabel = new java.awt.Label("To:");
- recipientLabel.reshape(14,38,96,23);
- keyPressManagerPanel1.add(recipientLabel);
- //}}
- }
-
- public boolean handleEvent(Event event) {
- if (event.target == Send && event.id == Event.ACTION_EVENT)
- {
- sendMessage();
- }
- if (event.target == newButton && event.id == Event.ACTION_EVENT)
- {
- prepForNewMessage();
- }
- return super.handleEvent(event);
- }
-
- //Resets all TextFields and textArea in preparation for a new message.
- void prepForNewMessage()
- {
- senderName.setText("");
- recipientName.setText("");
- subjectLine.setText("");
- messageBody.setText("");
- serverResponse.setText("");
- }
-
- //This is the method responsible for establishing a communications
- //path to the mail server and for sending SMTP commands.
- // Step 1: Create a new socket object using the name of the mail
- // server and the port number.
- // Step 2: Create a printstream object based on the new socket
- // Step 3: Send the mail server the appropriate SMTP commands
- // using the printstream.
- // Step 4: This sample also captures and displays status messages
- // return from the mail server. See getReply() method
- // below.
- // Step 5: Release the connection to the server.
- // Step 6: Close the printstream and the socket.
- void sendMessage()
- {
- //Insert sending host name below...
- String sHostName = new String("mailservername.company.com");
-
- //Insert sendmail port number on sending host machin below...
- //(Note: Port will typically be port #25.)
- int portNum = 25;
-
- try
- {
- //Step 1
- Socket outgoing = new Socket(sHostName, portNum, true);
-
- //Step 2
- PrintStream ps = new PrintStream(outgoing.getOutputStream());
-
- //Steps 3 & 4
- ps.println("HELO " + sHostName);
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- ps.println("MAIL FROM: " + senderName.getText());
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- ps.println("RCPT TO: " + recipientName.getText());
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- ps.println("DATA");
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- ps.println("Subject: " + subjectLine.getText() + "\n" + messageBody.getText() + "\n" + "." + "\n");
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- //Step 5
- ps.println("QUIT");
- serverResponse.appendText(getReply(outgoing) + "\n");
-
- //Step 6
- ps.close();
- outgoing.close();
- }
- catch (IOException Ex)
- {
- System.out.println(Ex.getMessage());
- }
- }
-
- //This method will capture status message returned by the mail server
- //and display in the the Server Response TextArea on the Applet.
- //Step 1: Create an InputStream for the socket object created above that gets passed
- // into this method.
- //Step 2: Read a line of the InputStream and return the line as a string for display.
- String getReply(Socket incoming)
- {
- try
- {
- //Step 1
- DataInputStream myDIS = new java.io.DataInputStream(incoming.getInputStream());
-
- //Step 2
- return myDIS.readLine();
- }
- catch (java.io.IOException Ex)
- {
- return Ex.getMessage();
- }
- }
-
- //{{DECLARE_CONTROLS
- java.awt.Label Title;
- symantec.itools.awt.KeyPressManagerPanel keyPressManagerPanel1;
- java.awt.TextField senderName;
- java.awt.TextField recipientName;
- java.awt.TextField subjectLine;
- java.awt.TextArea messageBody;
- java.awt.TextArea serverResponse;
- java.awt.Button Send;
- java.awt.Button newButton;
- java.awt.Label senderLabel;
- java.awt.Label messageLabel;
- java.awt.Label subjectLabel;
- java.awt.Label serverRepsonseLabel;
- java.awt.Label recipientLabel;
- //}}
- }
-